﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Objects;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using QuestionsAnswers.UI.Models;

namespace QuestionsAnswers.UI.Controllers
{
    public class CourseController : Controller
    {
        private readonly QuestionsAnswersContext _db = new QuestionsAnswersContext();

        [Authorize]
        public ViewResult Index()
        {
            User user = _db.Users.First(x => x.UserName == User.Identity.Name);
            return View(user);
        }

        [Authorize]
        public ActionResult Create()
        {
            return Request.IsAjaxRequest() ? (ActionResult)PartialView() : View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult Create(Course course, String emails, String memberType)
        {
            if (ModelState.IsValid)
            {
                course.Creator = _db.Users.First(x => x.UserName == User.Identity.Name);
                _db.Courses.Add(course);
                _db.SaveChanges();
                course.GetInvitations(emails, memberType, _db.Users.First(x => x.UserName == User.Identity.Name)).ForEach(inv => _db.Invitations.Add(inv));
                _db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(course);
        }

        [Authorize]
        public ActionResult Edit(int id)
        {
            Course course = _db.Courses.First(x => x.CourseId == id);
            return Request.IsAjaxRequest() ? (ActionResult)PartialView(course) : View(course);
        }


        [Authorize]
        [HttpPost]
        public ActionResult Edit(Course course)
        {
            if (ModelState.IsValid)
            {
                Course courseWithOwners = _db.Courses.First(x => x.CourseId == course.CourseId);

                if (courseWithOwners.CanModify(User.Identity.Name))
                {
                    _db.Entry(courseWithOwners).CurrentValues.SetValues(course);
                    _db.SaveChanges();
                }
                return RedirectToAction("Index");
            }
            return View(course);
        }

        public ActionResult Delete(int id)
        {
            Course course = _db.Courses.Find(id);
            return View(course);
        }


        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Course course = _db.Courses.Find(id);
            if (course.CanModify(User.Identity.Name))
            {
                _db.Courses.Remove(course);
                _db.SaveChanges();
            }

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }


        [Authorize]
        public ActionResult ManageMembers(int id)
        {
            var course = _db.Courses.First(x => x.CourseId == id);
            return View(course);
        }


        public ActionResult DeleteMember(int userId, int courseId, String memberType)
        {
            CourseMember member = new CourseMember
                                      {
                                          User = _db.Users.Find(userId),
                                          Course = _db.Courses.Find(courseId),
                                          MemberType = memberType
                                      };
            return View(member);
        }

        [HttpPost, ActionName("DeleteMember")]
        [Authorize]
        public ActionResult DeleteMemberConfirmed(int userId, int courseId, string memberType)
        {

            Course course = _db.Courses.Include("Owners").Include("Participants").First(x => x.CourseId == courseId);
            if (course.CanModify(User.Identity.Name))
            {
                if (memberType == "Owner")
                    course.Owners.Remove(_db.Users.Find(userId));
                else if (memberType == "Participants")
                    course.Participants.Remove((_db.Users.Find(userId)));

                _db.Entry(course).State = EntityState.Modified;
                _db.SaveChanges();
            }
            return View("ManageMembers", course);

        }


        public ActionResult CourseQuestions(int id)
        {
            return View(_db.Courses.First(x => x.CourseId == id));
        }
    }
}